package cn.ld.module.ai.dal.mapper.knowledge;

import cn.ld.framework.common.enums.CommonStatusEnum;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.mybatis.core.mapper.BaseMapperX;
import cn.ld.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ld.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgePageInVO;
import cn.ld.module.ai.dal.entity.knowledge.AiKnowledgeEntity;
import org.apache.ibatis.annotations.Mapper;

/**
 * AI 知识库基础信息 Mapper
 *
 * @author xiaoxin
 */
@Mapper
public interface AiKnowledgeMapper extends BaseMapperX<AiKnowledgeEntity> {

    default PageResult<AiKnowledgeEntity> selectPage(Long userId, AiKnowledgePageInVO pageInVO) {
        return selectPage(pageInVO, new LambdaQueryWrapperX<AiKnowledgeEntity>()
                .eq(AiKnowledgeEntity::getStatus, CommonStatusEnum.ENABLE.getStatus())
                .likeIfPresent(AiKnowledgeEntity::getName, pageInVO.getName())
                .and(e -> e.apply("FIND_IN_SET(" + userId + ",visibility_permissions)").or(m -> m.apply("FIND_IN_SET(-1,visibility_permissions)")))
                .orderByDesc(AiKnowledgeEntity::getId));
    }
}
